package top.blackr.egg;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.experimental.UtilityClass;
import net.dreamlu.mica.core.utils.ThreadUtil;
import net.dreamlu.mica.http.HttpRequest;

/**
 * 小插曲
 *
 * @author BlackR
 */
@UtilityClass
public class EasterEgg {

    /**
     * 随机获取沙雕文案（彩虹屁chp、毒鸡汤du、朋友圈pyq）
     *
     * @return 沙雕文案
     */
    public String getShaDiaoText() {
        String url = "https://api.shadiao.pro/%s";
        String type;
        int randomInt = RandomUtil.randomInt(1, 4);
        switch (randomInt) {
            case 2:
                type = ShaDiaoTypeEnum.du.name();
                break;
            case 3:
                type = ShaDiaoTypeEnum.pyq.name();
                break;
            default:
                type = ShaDiaoTypeEnum.chp.name();
        }
        String textStr = HttpRequest.get(String.format(url, type)).execute().asString();
        JSONObject entries = JSONUtil.parseObj(textStr);
        return entries.getJSONObject("data").getStr("text");
    }

    /**
     * <a href="https://developer.hitokoto.cn/sentence/#%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E">随机获取每日一言</a>
     * <br/> QPS 3.5
     * @return 每日一言
     */
    public String getOneTalk() {
        String url = "https://v1.hitokoto.cn/";
        String oneTalk = HttpRequest.get(url).execute().asString();
        JSONObject entries = JSONUtil.parseObj(oneTalk);
        String from = entries.getStr("from");
        String hitokoto = entries.getStr("hitokoto");
        ThreadUtil.sleep(500);
        return hitokoto + "\t—— " + from;
    }
}
